package com.zprmb.udzt.fragment;

import java.text.DecimalFormat;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zprmb.udzt.DetailsActivity;
import com.zprmb.udzt.HomeActivity;
import com.zprmb.udzt.HomeActivity.Refresh;
import com.zprmb.udzt.R;
import com.zprmb.udzt.bean.HomeProduct;

public class HometFragmet extends Fragment {
	public String name,id,rate,rate_add,time,pro_type,red,desc="",isFlagFull="",status;
	private boolean flag;
	public static HomeProduct productl=new HomeProduct();
	private TextView product_name,product_rate,tv_rate_add,pro_time,tv_coupon_type,new_user,jiahao,btn_desc;
    private 	View view;
    private TextView tv_tag1,tv_tag2,tv_tag3;
    private String tag1,tag2,tag3;
    private Button btn_invest;
    DecimalFormat df = new DecimalFormat("###0.00");  
    DecimalFormat df2 = new DecimalFormat("###0.0");  
    private LinearLayout li_jiaxi,li_cu;
    private boolean isResume = true,isred,flagFill;
    private RelativeLayout rl_time;
    SharedPreferences sha;
   private setCurrentTabByTagListener currentTabByTagListener;
    public void setListener(setCurrentTabByTagListener setCurrentTabByTagListener){
    	currentTabByTagListener=setCurrentTabByTagListener;
    }
    
	
	public static HometFragmet newInstance(HomeProduct product,List<String> list,String desc,HomeActivity homeActivity) {
		HometFragmet uf = new HometFragmet();
		Bundle b = new Bundle();
		b.putString("name", product.getProductName());
		b.putString("id", product.getProductId());
		b.putString("rateadd", product.getRateAdd());
		b.putString("period", product.getProductPeriod());
		b.putString("rate", product.getRate());
		b.putString("productId", product.getProductId());
		b.putString("producttype", product.getProductType());
		b.putBoolean("flag", product.isFlagFresh());
		b.putString("red", product.getRedPacketRate());
		b.putString("isFlagFull", product.getIsFlagFill());
		b.putString("assetLoanStatus", product.getAssetLoanStatus());
		b.putBoolean("flagFill", product.isFlagFill());
		try {
			b.putString("tag1", list.get(0).toString()); 
			b.putString("tag2", list.get(1).toString());
			b.putString("tag3", list.get(2).toString());
			b.putString("desc", desc);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
//		b.putString("", value)
		uf.setArguments(b);
		
   Log.e("newintance", "newintance");
		return uf;
	}
	public void onrefresh(){
		
	}
	public void setlist(HomeProduct product,List<String> list,String desc,HomeActivity homeActivity){
		name=product.getProductName();
		id=product.getProductId();
		time=product.getProductPeriod();
		pro_type=product.getProductType();		
		rate=product.getRate();
		rate_add=product.getRateAdd();
		flag=product.isFlagFresh();
		red=product.getRedPacketRate();
		
		tag1=list.get(0);
		tag2=list.get(1);
		tag3=list.get(2);
		desc=desc;
		isFlagFull=product.getIsFlagFill();
		flagFill=product.isFlagFill();
		Log.e("刷新", name);
	}
	@Override
	@Deprecated
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		
		try {
			currentTabByTagListener=(setCurrentTabByTagListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
	}
	

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		status=getArguments().getString("assetLoanStatus");
		name=getArguments().getString("name");
		id=getArguments().getString("id");
		time=getArguments().getString("period");
		pro_type=getArguments().getString("producttype");
		rate=getArguments().getString("rate");
		rate_add=getArguments().getString("rateadd");
		flag=getArguments().getBoolean("flag");
		red=getArguments().getString("red");
		isred=getArguments().getBoolean("isred");
		tag1=getArguments().getString("tag1");
		tag2=getArguments().getString("tag2");
		tag3=getArguments().getString("tag3");
		desc=getArguments().getString("desc");
		isFlagFull=getArguments().getString("isFlagFull");
		flagFill=getArguments().getBoolean("flagFill");
		
		Log.e("creat", "creat");
	}
	
	
	public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
		 view = inflater.inflate(R.layout.home_fragment, container,
				false);
		 
		
		initview();
		sha=getActivity().getSharedPreferences("config", getActivity().MODE_PRIVATE);
		product_name.setText(name);	    
	    product_rate.setText(df.format(Double.parseDouble(rate)-Double.parseDouble(rate_add))+"%");
	    pro_time.setText("期限"+time);
	    tv_rate_add.setText(df.format(Double.parseDouble(rate_add))+"%");
	    if(flag){
	    	new_user.setVisibility(View.VISIBLE);
	    	tv_rate_add.setText(df.format(Double.parseDouble(red))+"%");
	    	tv_coupon_type.setText("新人加息券");
	    	li_jiaxi.setBackgroundResource(R.drawable.bjt);
	    }else{
	    	new_user.setVisibility(View.GONE);
	    	tv_coupon_type.setText("加息");
	    	li_jiaxi.setBackgroundResource(R.drawable.bjt);
	    }
	    if(Double.parseDouble(rate_add)<=0&&red==null){
	    	li_jiaxi.setVisibility(View.GONE);
	    	jiahao.setVisibility(View.GONE);
	    }  
//	    if(name!=null&&!name.equals("")&&name.equals("新人福利计划")){
//	    	tv_coupon_type.setText("s新人加息券");
//	    	li_jiaxi.setBackgroundResource(R.drawable.hbfx);
//	    }else{
//	    	tv_coupon_type.setText("加息");
//	    	li_jiaxi.setBackgroundResource(R.drawable.hbfx);
//	    }
	    
	    if(pro_type.equals("CURRENT_DEPOSIT")){
	    	li_cu.setVisibility(View.VISIBLE);
	    	li_jiaxi.setVisibility(View.GONE);
	    	rl_time.setVisibility(View.GONE);
	    	 pro_time.setVisibility(View.GONE);
	    	 product_name.setText("智投宝");
	    	 if(desc.equals("")){
	    		 btn_desc.setVisibility(View.GONE);
	    	 }else{
	    		 btn_desc.setVisibility(View.VISIBLE);
	    		 btn_desc.setText(desc);
	    	 }
	    	 jiahao.setVisibility(View.GONE);
	    	 
	    	 tv_tag1.setText(tag1);
	    	 tv_tag2.setText(tag2);
	    	 tv_tag3.setText(tag3);
	    	 
	    }else{
	    	
	    	 btn_desc.setVisibility(View.GONE);
	    	li_cu.setVisibility(View.GONE);
	    	rl_time.setVisibility(View.VISIBLE);
	    	 pro_time.setVisibility(View.VISIBLE);
	    	 if(status!=null){
	    		 
	    		 if(status.equals("COLLECTING")){
	    			 btn_invest.setText("马上投资");
	    			 btn_invest.setBackgroundResource(R.drawable.my_button);
	    		 }else{
	    			 btn_invest.setBackgroundResource(R.drawable.btn_unclick);
	    			 btn_invest.setEnabled(false);
	    			 btn_invest.setText("募集结束");
	    		 }
	    	 }
	    }
	    
//		if(flagFill){
//			if(pro_type.equals("FIXED_DEPOSIT_3")||pro_type.equals("MONTH_WIN")){
//				
//				btn_invest.setBackgroundResource(R.drawable.btn_unclick);
//				btn_invest.setText(isFlagFull);
//			}else{
//				btn_invest.setBackgroundResource(R.drawable.btn_unclick);
//				btn_invest.setText("已售罄");
//			}
//		}else{
//			btn_invest.setBackgroundResource(R.drawable.my_button);
//			btn_invest.setText("马上投资");
//		}
		
		btn_invest.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(pro_type.equals("CURRENT_DEPOSIT")){
					Editor ed=sha.edit();
					ed.putInt("tab", 1);
					ed.commit();
					currentTabByTagListener.setcurrent();
				}else{
					
					Intent intent=new Intent(getActivity(),DetailsActivity.class);
					intent.putExtra("type", pro_type);
					intent.putExtra("id", id);
					intent.putExtra("flag", flag );
					intent.putExtra("name", name);
					intent.putExtra("isred", isred);
					startActivity(intent);
				}
			}
		});
		return view;
	}
 public	interface setCurrentTabByTagListener{
	 public void setcurrent();
	}
	
	private void initview() {
		// TODO Auto-generated method stub
		product_name=(TextView)view.findViewById(R.id.home_pro_name);
		product_rate=(TextView)view.findViewById(R.id.home_rate);
		pro_time=(TextView)view.findViewById(R.id.home_pro_time);
		tv_coupon_type=(TextView)view.findViewById(R.id.coupon_type);
		tv_rate_add=(TextView)view.findViewById(R.id.home_rateadd);
		btn_invest=(Button)view.findViewById(R.id.btn_home_invest);
		li_jiaxi=(LinearLayout)view.findViewById(R.id.li_jiaxi);
		btn_desc=(TextView)view.findViewById(R.id.btn_desc);
		new_user=(TextView)view.findViewById(R.id.new_user);
		jiahao=(TextView)view.findViewById(R.id.jiahao);
		li_cu=(LinearLayout)view.findViewById(R.id.li_cu);
		tv_tag1=(TextView)view.findViewById(R.id.tv_tag1);
		tv_tag2=(TextView)view.findViewById(R.id.tv_tag2);
		tv_tag3=(TextView)view.findViewById(R.id.tv_tag3);
		rl_time=(RelativeLayout)view.findViewById(R.id.rl_time);
		
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

}
